package de.hauck.rtmidlet.views;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;

import de.hauck.rtmidlet.FileSystemShowLogThread;
import de.hauck.rtmidlet.RTMidlet;
import de.hauck.rtmidlet.model.Configuration;
import de.hauck.rtmidlet.utils.FileChooser;
import de.hauck.rtmidlet.utils.MyLogger;

public class FormDebug extends Form implements CommandListener, ItemCommandListener {
	private Configuration c;
	private RTMidlet _main;
	private FormSettingsMenu _fm;
	private ChoiceGroup debugLevelChoicegroup;
	private ChoiceGroup traceChoiceGroup;
	private String[] list = {"Path"};
	private String[] showLogListArray = {"Show Trace"};
	private ChoiceGroup pathToDebugList = new ChoiceGroup("Path to export", Choice.EXCLUSIVE, list, null);
	private ChoiceGroup showLogList = new ChoiceGroup("S", Choice.EXCLUSIVE, showLogListArray, null);
	private Command cSave = new Command("Save", Command.OK, 1);
	private Command cSelectPath = new Command("Select Path", Command.OK, 1);
	private Command cCancel = new Command("Cancel", Command.BACK, 1);
	private Command cBack = new Command("Back", Command.BACK, 1);
	private Command cShowTrace = new Command("Show Trace", Command.OK, 1);
	private static MyLogger logger;
	
	public FormDebug(RTMidlet rtmidlet, FormSettingsMenu fm) {
		super("Debug Settings");
		logger = MyLogger.getLogger();
		_main=rtmidlet;
		_fm=fm;
		debugLevelChoicegroup = new ChoiceGroup("Trace Level", ChoiceGroup.EXCLUSIVE);
		debugLevelChoicegroup.append("ALL", null);
		debugLevelChoicegroup.append("DEBUG", null);
		debugLevelChoicegroup.append("INFO", null);
		debugLevelChoicegroup.append("WARNING", null);
		debugLevelChoicegroup.append("ERROR", null);
		debugLevelChoicegroup.append("FATAL", null);
		debugLevelChoicegroup.setSelectedIndex(c.logLevel, true);
		traceChoiceGroup = new ChoiceGroup("Activate Trace", ChoiceGroup.MULTIPLE);
		traceChoiceGroup.append("Trace on?", null);
		if (c.logOn) traceChoiceGroup.setSelectedIndex(0, true);
		debugLevelChoicegroup.setSelectedIndex(c.logLevel, true);
		append(traceChoiceGroup);
		append(pathToDebugList);
		//list[0]=c.exportPath;
		String exportPath="<not selected>";
		//if (c.exportPath!="") exportPath=c.exportPath;
		pathToDebugList.set(0, exportPath, null);
		pathToDebugList.addCommand(cSelectPath);
		//pathToDebugList.setDefaultCommand(cSave2);
		pathToDebugList.setItemCommandListener(this);
		append(debugLevelChoicegroup);
		showLogList.addCommand(cShowTrace);
		showLogList.setItemCommandListener(this);
		append(showLogList);	
		addCommand(cSave);
		addCommand(cCancel);
		setCommandListener(this);
		_main.show(this);		
	}

	public void commandAction(Command command, Displayable dble) {
		if (command==cSave) {
			Configuration.dirtyFlagConfig=true;
			if (traceChoiceGroup.isSelected(0)) {
				c.logLevel=debugLevelChoicegroup.getSelectedIndex();
				c.logOn = true;
				logger.setLoggingLevel(debugLevelChoicegroup.getSelectedIndex());
				MyLogger.debug("Set Trace on - Level: "+debugLevelChoicegroup.getSelectedIndex());
			} else c.logOn=false;
			_main.show(_fm.getSettingsMenu());
		} else if (command == cCancel) {
			_main.show(_fm.getSettingsMenu());
		} else if (command==cBack) {
			_main.show(this);
		}
		
	}
	
	public void commandAction(Command command, Item item) {
		// ItemCommandListener
		if (command==cSelectPath) {
			FileChooser fchoser = new FileChooser(_main,c.exportPath,false,this);
			_main.show(fchoser);		
		} else if (command==cShowTrace) {
			String logFile = "file:///"+c.exportPath+"RTMidlet.txt";
			MyLogger.debug("Log file to show: "+logFile);
			new FileSystemShowLogThread(logFile, this);
		} 
		
	}
	
	public void showForm(String path) {
		pathToDebugList.set(0, path, null);
		_main.show(this);
	}
	
	public void showTrace(String trace) {
		//_trace = trace;
		TextBox logText = new TextBox("Trace", trace, trace.length(), TextField.ANY);
		logText.addCommand(cBack);
		logText.setCommandListener(this);
		_main.show(logText);
	}

}
